package shc.graduation.http.getter;

import android.text.TextUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import shc.graduation.bean.CodeInfo;
import shc.graduation.utils.log.LogUtils;

/**
 * Created by shuhuiChen on  2016/5/21  17:53.
 * Email ：shuhuiCSH@126.com
 */
public class CodeInfoGetter extends BaseGetter<CodeInfo> {


    @Override
    public List<CodeInfo> getModel(String websiteUrl) {

        String webContent = mIHttpGet.getContent(websiteUrl);

        if (TextUtils.isEmpty(webContent)) {
            return null;
        }

        mListModel = new ArrayList<>();

        Document doc = Jsoup.parse(webContent);
        Elements trs = doc.select("li[class=codeli]");

        iteratorCodeInfo(trs);

        return mListModel;
    }

    /**
     * 迭代遍历CodeInfo
     *
     * @param trs
     */
    private void iteratorCodeInfo(Elements trs) {
        CodeInfo codeInfo;
        Elements elements = null;
        boolean isCanSave;
        for (Element tr : trs) {

            codeInfo = new CodeInfo();

            isCanSave = false;

            elements = tr.select("div[class=codeli-photo]");
            if (elements != null && elements.size() > 0) {
                parsePhoto(codeInfo, elements.get(0));
            }

            elements = tr.select("div[class=codeli-info]");
            if (elements != null && elements.size() > 0) {
                isCanSave = parseCodeInfo(codeInfo, elements.get(0));
            }

            if (isCanSave) {
                mListModel.add(codeInfo);
            }
        }
        if (trs != null && trs.size() > 0) {
            trs.clear();
        }
    }

    private void parsePhoto(CodeInfo codeInfo, Element element) {
        Elements elements = null;
        elements = element.getElementsByTag("img");

        if (elements == null || elements.size() <= 0) {
            return;
        }

        String photoUrl = elements.get(0).attr("src") + "";
        codeInfo.setPhotoUrl(photoUrl);
        LogUtils.e("图片Url：" + photoUrl);
    }

    private boolean parseCodeInfo(CodeInfo codeInfo, Element tr) {

        Elements elements = null;
        boolean isCanSave = false;

        elements = tr.select("h2[class=codeli-name]");
        if (elements == null || elements.size() <= 0) {
            return isCanSave;
        }
        isCanSave = parseNameAndContentUrl(codeInfo, elements.get(0));

        elements = tr.select("p[class=codeli-description]");
        if (elements != null && elements.size() > 0) {
            parseDescription(codeInfo, elements.get(0));
        }

        elements = tr.select("div[class=otherinfo]");
        if (elements != null && elements.size() > 0) {
            parseOtherInfo(codeInfo, elements.get(0));
        }

        return isCanSave;
    }

    private boolean parseNameAndContentUrl(CodeInfo codeInfo, Element element) {
        Elements elements = null;
        boolean isCanSave = false;

        elements = element.getElementsByTag("a");
        if (elements == null || elements.size() <= 0) {
            return isCanSave;
        }
        codeInfo.setContentUrl(elements.get(0).attr("href") + "");
        codeInfo.setName(elements.get(0).text() + "");
        LogUtils.e("内容Url：" + elements.get(0).attr("href") + "");
        LogUtils.e("名字：" + elements.get(0).text() + "");

        isCanSave = true;

        return isCanSave;
    }

    private void parseDescription(CodeInfo codeInfo, Element element) {
        String content = element.text() + "";
        codeInfo.setContent(content + "");
        LogUtils.e("内容：" + content);
    }

    private void parseOtherInfo(CodeInfo codeInfo, Element element) {

        Elements elements = null;

        elements = element.getElementsByTag("div");
        if (elements == null || elements.size() <= 0) {
            return;
        }

        Elements emts = elements.get(0).getElementsByTag("a");
        if (emts != null && emts.size() > 0) {
            String groupName = emts.get(0).text().replaceAll(" ", "");
            codeInfo.setGroupName(groupName + "");
            LogUtils.e("组名：" + groupName);
        }

        //解析时间
        if (elements.size() > 2) {

            emts = elements.get(2).getElementsByTag("span");
            if (emts != null && emts.size() > 0) {
                String allStr = emts.get(0).text().toString() + "";

                LogUtils.e("时间混合字符串：" + allStr);

                String matchStr = "[0-9]{4}-[0-9]{2}-[0-9]{2}";
                Pattern pattern = Pattern.compile(matchStr);
                Matcher matcher = pattern.matcher(allStr);
                matcher.find();

                String time = matcher.group(0);
                if (!TextUtils.isEmpty(time)) {
                    codeInfo.setTime(time);
                    LogUtils.e("时间：" + time);
                } else {
                    LogUtils.e("时间为空,找不到");
                }
            } else {
                LogUtils.e("时间span为空,找不到");
            }
        } else {
            LogUtils.e("没有时间div");
        }
    }

    @Override
    public String getEncode() {
        return "GB2312";
    }

}
